package com.lemon.stock.net

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

object ApiRequest {
    const val TAG = "ApiRequest"
}

suspend inline fun <T> apiCall(crossinline call: suspend CoroutineScope.() -> ApiResponse<T>) : ApiResponse<T> {
    return withContext(Dispatchers.IO) {
        val res: ApiResponse<T>
        try {
            res = call()
        } catch (e: Throwable) {
            return@withContext ApiResponse.fromException(e)
        }
        return@withContext res
    }
}